<?php
abstract class Mozg_Response
{
	const StatusOk = 0;
	const StatusError = 1;
	
	protected $_status;
    protected $_body = '';
    
    public function __construct ()
    {
    	$this->_status = self::StatusOk;
        $this->RegisterOutputFilter('absoluteUrl', array($this, 'SetAbsoluteUrl'));
    }

    /**
		устанавливает тело ответа
     */
    public function SetBody ($body)
    {
        $this->_body = $body;
        return $this;
    }
    /**
		возвращает тело ответа
     */
    public function GetBody ()
    {
        return $this->_body;
    }
    
    public function SetStatus($status = 0)
    {
    	$this->_status = $status;
    	return $this;
    }

    public function GetStatus()
    {
    	return $this->_status;
    }
    
    static private $_outputFilters = array();
    
    public function RegisterOutputFilter($name, $callback)
    {
    	self::$_outputFilters[$name] = $callback;
    }
    
    protected function RunOutputFilters()
    {
    	$body = $this->GetBody();
    	foreach (self::$_outputFilters as $callback)
    	{
    		$body = call_user_func($callback, $body);
    	}
    	
    	self::$_outputFilters = array();
    	
    	$this->SetBody($body);
    }
    
    /**
     * Send the response, including all headers, rendering exceptions if so
     * requested.
     *
     * @return void
     */
    public function GetResponse()
    {
    	$this->RunOutputFilters();
        return $this->GetBody();
    }
    
    public function SendResponse ()
    {
		echo $this->GetResponse();
    }
    
    public function SetAbsoluteUrl($content)
    {
    	return Mozg::Lib('str')->SetAbsoluteUrls($content);
    }
}